# from src.grpc.clients.robot_data.robot_data_client import RobotDataClient
# from src.consul.consul_client import ConsulClient
# from src.config.config import Config
# import requests

# ROBOT_IP = '192.168.1.159'
# ROBOT_PORT = 50051
# TASK_ID = 1

# consul_client = ConsulClient()
# robot_data_client = RobotDataClient(ROBOT_IP, ROBOT_PORT)

# # SERVER_CONNECTOR_IP, SERVER_CONNECTOR_PORT = consul_client.get_service_address('server connector')
# SERVER_CONNECTOR_IP = '10.10.11.10'
# SERVER_CONNECTOR_PORT = 8080

# config = Config()

# def connect_consul():
#     consul_client.consul_ip = config.consul_ip
#     consul_client.consul_port = config.consul_port

# def check_task():
#     url = f"http://{SERVER_CONNECTOR_IP}:{SERVER_CONNECTOR_PORT}/scheme/check/{TASK_ID}"
#     response = requests.get(url)

#     if response.status_code == 200:
#         if response.text == 'OK':
#             return True
#     return False

# def start_task():
#     url = f"http://{SERVER_CONNECTOR_IP}:{SERVER_CONNECTOR_PORT}/scheme/start/{TASK_ID}"
#     response = requests.get(url)

#     if response.status_code == 200:
#         if response.text == 'OK':
#             return True
#     return False

# if __name__ == '__main__':
#     if not check_task():
#         if not start_task():
#             exit()

# # while True:
# #     user_input = input("请输入内容（按Enter键结束）: ")
# #     robot_data_client.set_twist_keyboard(user_input)
# #     print("您输入的内容是:", user_input)

from src.config.config import Config
from src.grpc.servers.grpc_server_builder import GRPCServerBuilder
import time
from src.grpc.servers.service_coordinator.service_coordinator_server import ServiceCoordinatorServer
import yaml

service_coordinator_server = ServiceCoordinatorServer()
config = Config()
gRPCServer = None

def connect_consul():
    from src.consul import ConsulClient
    from src.consul import ServiceInfo
    from src.utils import get_local_ip_address

    consul_client = ConsulClient()
    consul_client.consul_ip = config.consul_ip
    consul_client.consul_port = config.consul_port
    host = get_local_ip_address()
    service_name = config.service_name
    service_port = config.service_port
    service_info = ServiceInfo()
    service_info.service_ip = host
    service_info.service_port = service_port
    service_info.service_name = service_name
    service_info.service_id = f'{service_name}-{host}-{service_port}'
    service_info.service_tags = config.service_tags
    consul_client.register_service(service_info)

def gRPC_server_start():
    global gRPCServer
    gRPCServerBuilder = GRPCServerBuilder()
    gRPCServer = gRPCServerBuilder.build()
    service_coordinator_server.join_in_server(gRPCServer)
    gRPCServer.start()

if __name__ == '__main__':
    connect_consul()
    gRPC_server_start()

    while True:
        time.sleep(60 * 60 * 24)
